﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2022 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Base;

/*
 * 单例模式
 https://www.donet5.com/Home/Doc?typeId=2352
 */

/// <summary>
/// sqlsugar 单例模式，可继承，可静态方式使用
/// </summary>
public class SqlSugarHelper //不能是泛型类
{
    //  //用单例模式
    //  public static SqlSugarScope Db = new SqlSugarScope(SqlSugarCore.GetConnectionConfig(),
    //db => db.SqlSugarConfig());

    //用单例模式
    public static SqlSugarScope Db = new SqlSugarScope(SqlSugarCore.SqlSugarDbSetting.ConnectionConfigs
  , db => SqlSugarCore.SqlSugarClientAction(db));
}

/*
使用方式1
继承方式单例
public class TestManager : DbContext
{
    public List<Order> Add()
    {
        return Db.Queryable<Order>().ToList();
    }
}
public class DbContext  //如果是泛型类 Db要扔到外面 ,DbContext<T>.Db会导致产生多个实例
{
    protected  static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.SqlServer,
        ConnectionString = Config.ConnectionString,
        IsAutoCloseConnection = true
    },
     db => {
          //单例参数配置，所有上下文生效
          db.Aop.OnLogExecuting = (s, p) =>
         {
             Console.WriteLine(s);
         };
     });
}
*/

/*
类调用方式 （静态）
public class TestManager
{
    public List<Order> Add()
    {
        return DbContext.Db.Queryable<Order>().ToList();
    }
}
public class DbContext //如果是泛型类 Db要扔到外面 ,DbContext<T>.Db会导致产生多个实例
{
        //这里要public
    public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.SqlServer,
        ConnectionString = Config.ConnectionString,
        IsAutoCloseConnection = true
    },
     db => {
          //单例参数配置，所有上下文生效
          db.Aop.OnLogExecuting = (s, p) =>
         {
             Console.WriteLine(s);
         };
     });
}
*/